<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<div>
    <h1>C++ - MPGs<br /> </h1>
    <?php
        if (isset($_GET['mpgcode'])) {
            $usercode = $_GET['mpgcode'];
        }
        $part1="#include <iostream>
        using namespace std;

        int main(){
            float milesTraveled=45.6;
            float gallonsUsed=2.5;
            float milesPerGallon; 
            /* Calculate miles per gallon below using the predefined variables*/";

            $part2="cout << \"Miles per gallon = \" << milesPerGallon << endl;
            return 0;
        }";


        $allcode= $part1 . $usercode . $part2;
        $myFile = "mpg.cpp";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $allcode);
        fclose($fh);


        $command = exec('g++ -o test mpg.cpp 2>&1');
        echo $command . "\n";

        $command = exec('./test');
        echo $command . "\n";
    ?>
</div>
</body>
</html>
